<template>
	<view>
		<view class="table">
			<form @submit="submitData" @reset="resetForm">
				<uni-table border stripe emptyText="暂无更多数据">
					<uni-tr>
						<uni-th align="center" width="20">题号</uni-th>
						<uni-th align="center" width="130">题目</uni-th>
						<uni-th align="center">选项<br />(0=完全不会,1=好几天,2=一半以上的天数,3=几乎每天)</uni-th>
					</uni-tr>
					<uni-tr v-for="(item1,index) in tableData" :key="index">
						<uni-td>{{item1.questionId}}</uni-td>
						<uni-td>{{item1.questionContent}}</uni-td>
						<uni-td>
							<view class="uni-list">
								<radio-group @change="radioChange($event,item1)">
									<label class="uni-list-cell uni-list-cell-pd"
										v-for="(item2, index) in item1.options" :key="item2.value">
										<view>
											<radio :value="item2.option" :checked="index===current">
												{{item2.optionContent}}</radio>
										</view>
									</label>
								</radio-group>
							</view>
						</uni-td>
					</uni-tr>
				</uni-table>
				<button form-type="submit">Submit</button>
				<button form-type="reset">Reset</button>
			</form>
		</view>
	</view>
</template>

<script>
	import {
		uniTable,
		uniTr,
		uniTh,
		uniTd
	} from '@dcloudio/uni-ui'
	export default {
		onLoad(options) {
			// console.log(options)
			this.scoreData.examId = options.id
		},
		data() {
			return {
				tableData: {},
				// 返回的table
				scoreData: {
					examId:'',
					scoreList: []
				},
				current: '',
				
			}
		},
		created() {
			this.getData()
		},
		mounted() {

		},
		components: {
			uniTable,
			uniTr,
			uniTh,
			uniTd
		},
		methods: {
			async getData() {
				let token = uni.getStorageSync('uni_id_token')
				// console.log(token)
				let res = await this.$axios.get(`/api/exam/get/${this.scoreData.examId}`)
				// console.log(res)
				this.tableData = res.data.questions
				// console.log(this.tableData)
			},
			radioChange($event, item1) {
				item1.checkedOptionId = $event.detail.value
			},
			async submitData(e) {
				//console.log(this.tableData)
				this.tableData.forEach(item=>{
					//console.log(item)
					let extraObj = {}
					extraObj.questionId = item.questionId
					if('checkedOptionId' in item){
						extraObj.option = item.checkedOptionId
					}else{
						extraObj.option=''
					}
					this.scoreData.scoreList.push(extraObj)
					extraObj={}
				})
				uni.navigateBack({
					delta: 1
				})
				// console.log(this.scoreData)
				let res = await this.$axios.post(`/api/exam/submit`,this.scoreData)
				console.log(res)

			},
			resetForm($event) {
				console.log($event)
			}
		}
	}
</script>

<style>

</style>
